package com.woniu.service.imp;

import com.woniu.dao.ShippingMapper;
import com.woniu.dao.UsersMapper;
import com.woniu.pojo.Shipping;
import com.woniu.pojo.Users;
import com.woniu.service.UsersService;
import com.woniu.util.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UsersServiceImp implements UsersService {

    @Autowired
    UsersMapper usersMapper;

    @Autowired
    ShippingMapper shippingMapper;

    @Override
    public Users getUser(long id)
    {
        return usersMapper.getPersonById(id);
    }

    @Override
    public String update(Users users)
    {
        int line = usersMapper.updateByPrimaryKeySelective(users);
        if (line != 0)
        {
            return "修改成功";
        } else
        {
            return "修改失败";
        }
    }

    @Override
    public List<Shipping> getRecAddress(Long id)
    {
        return shippingMapper.getRecAddress(id);
    }

    @Override
    public String delRecAddress(int id)
    {
        int i = shippingMapper.deleteByPrimaryKey(id);
        if (i != 0)
        {
            return "删除成功";
        } else
        {
            return "删除失败";
        }
    }

    @Override
    public String updateRecAddress(Shipping shipping)
    {
        int i = shippingMapper.updateByPrimaryKey(shipping);
        if (i != 0)
        {
            return "修改成功";
        } else
        {
            return "修改失败";
        }
    }

    @Override
    public String getOldPass(Long id)
    {
        return usersMapper.getPasswordBYID(id);
    }

    @Override
    public int updatePass(Long id, String newPass,String oldPass)
    {
        return usersMapper.updatePassById(id, newPass);
    }

    @Override
    public String updateByOther(Integer id, String userName, String birthday, String birthPlace, String phoneNumber, String headimg)
    {
        int i = usersMapper.updateByOther(id, userName, birthday, birthPlace, phoneNumber,headimg);
        if (i != 0)
        {
            return "修改成功";
        } else
        {
            return "修改失败";
        }
    }

    @Override
    public Map Login(String acc, String pwd)
    {
        Map map = new HashMap();
        List list = usersMapper.getPIByAc(acc);
        if (list.get(1).equals(MD5Util.getMD5(pwd)))
        {
            map.put("msg", "登录成功");
            map.put("id", list.get(0));
            return map;
        } else
        {
            map.put("msg", "登录失败");
            return map;
        }
    }

    @Override
    public String regist(String acc, String pwd)
    {
        String password = usersMapper.register(acc, MD5Util.getMD5(pwd));
        if (password.equals(pwd))
        {
            return "注册成功";
        } else
        {
            return "注册失败";
        }
    }
}
